﻿#include "pl_additem.h"
#include "ui_pl_additem.h"

PL_AddItem::PL_AddItem(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::PL_AddItem)
{
    ui->setupUi(this);

    table=TD_PURCHASE_LOG;
    updateCategory();
}

PL_AddItem::~PL_AddItem()
{
    delete ui;
}

void PL_AddItem::setCategory(const QString &category)
{
    ui->categories->setCurrentText(category);
}

void PL_AddItem::updateCategory()
{
    QStringList items=sp_getCategory(table);
    if(!items.isEmpty())
        ui->categories->addItems(items);

    ui->categories->addItem(MANUAL_INPUT);
    ui->categories->setCurrentIndex(-1);
}

void PL_AddItem::on_categories_currentTextChanged(const QString &text)
{
    if(text == MANUAL_INPUT)
    {
        ui->category->clear();
        ui->category->setEnabled(true);
    }
    else
    {
        ui->category->setText(text);
        ui->category->setEnabled(false);
    }
}

void PL_AddItem::on_btn_copy_clicked()
{
    if(ui->btn_copy->text() == "V")
    {
        QString url=qApp->clipboard()->text();
        if(!url.isEmpty())
            ui->url->setText(url);
    }
    else
    {
        QString url=ui->url->text().trimmed();
        if(!url.isEmpty())
            QDesktopServices::openUrl(QUrl(url));
    }
}

void PL_AddItem::setItem(const QString &category, const QString &label,
                         const QString &model, const QString &material,
                         const QString &url)
{
    ui->categories->setCurrentText(category);

    if(!label.isEmpty())
        ui->label->setText(label);
    if(!model.isEmpty())
        ui->model->setText(model);
    if(!material.isEmpty())
        ui->material->setText(material);
    if(!url.isEmpty())
    {
        ui->url->setText(url);
        ui->btn_copy->setText("Open");
    }

    ui->number->setFocus();
}

void PL_AddItem::on_btn_submit_clicked()
{
    QString category=ui->category->text().trimmed();
    if(category.isEmpty())
    {
        MESSAGE_CATEGORY_EMPTY
    }

    QString label=ui->label->text().trimmed();
    if(label.isEmpty())
    {
        MESSAGE_LABEL_EMPTY
    }

    QString model=ui->model->text().trimmed();
    QString material=ui->material->text().trimmed();

    QString numberStr=ui->number->text().trimmed();
    QString priceStr=ui->price->text().trimmed();
    if(numberStr.isEmpty() || priceStr.isEmpty())
    {
        QMessageBox::warning(this,QStringLiteral("输入无效"),
                             QStringLiteral("数量、单价不能为空！"));
        return;
    }

    double number=numberStr.toDouble();
    double price=priceStr.toDouble();
    if(number == 0 || price == 0)
    {
        QMessageBox::warning(this,QStringLiteral("输入无效"),
                             QStringLiteral("数量、单价只能为数字！"));
        return;
    }

    double total=number * price;
    QString url=ui->url->text().trimmed();
    QString remark=ui->remark->text().trimmed();

    if(addItem(category,label,model,material,
               number,price,total,url,remark))
        accept();
    else
        MESSAGE_CANNOT_SUBMIT
}

bool PL_AddItem::addItem(const QString &category, const QString &label,
                         const QString &model, const QString &material,
                         double number, double price, double total,
                         const QString &url, const QString &remark)
{
    QSqlQuery query;
    query.prepare(QString("insert into %1 (date_,category,label,model,material,"
                          "number,price,total,url,remark) values (?,?,?,?,?,?,?,?,?,?)")
                  .arg(TD_PURCHASE_LOG));
    query.addBindValue(Today);
    query.addBindValue(category);
    query.addBindValue(label);
    query.addBindValue(model);
    query.addBindValue(material);
    query.addBindValue(number);
    query.addBindValue(price);
    query.addBindValue(total);
    query.addBindValue(url);
    query.addBindValue(remark);
    if(query.exec())
        return true;
    else
    {
        qDebug() << query.lastError().text();
        return false;
    }
}

void PL_AddItem::on_price_editingFinished()
{
    double number=ui->number->text().toDouble();
    double price=ui->price->text().toDouble();
    if(number == 0 || price == 0)
        return;

    double total=number * price;
    ui->total->setText(QString::number(total));
}
